package com.fun.feifun.base.utils.http;

import com.alibaba.fastjson.JSONObject;
import com.fun.feifun.base.result.Result;
import com.fun.feifun.base.utils.common.ListUtil;
import com.fun.feifun.base.utils.format.FormatUtil;
import com.fun.feifun.modules.business.model.xml.OrderParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wukefei
 * @date $ $
 */
public class HttpUtil {
    public static String odPost(String url, Object object,String content_type) {
        String responseBody ="";
        try {
            // 创建HttpClient实例
            HttpClient httpClient = HttpClients.createDefault();
            // 创建HttpPost实例
            HttpPost httpPost = new HttpPost(url);
            // 设置请求头部的Content-Type
            if(StringUtils.isNotEmpty(content_type)){
                httpPost.setHeader("Content-Type", content_type);
            }
            httpPost.setHeader("Content-Type", "application/json");
            // 创建一个JSONObject对象
            String jsonString = FormatUtil.objToJson(object);
            System.out.println("请求接口 :" + url);
            System.out.println("请求参数 :" + jsonString);
            // 将JSONObject转换为JSON字符串
            StringEntity requestEntity = new StringEntity(jsonString);
            httpPost.setEntity(requestEntity);
            // 执行请求并获得响应
            HttpResponse response = httpClient.execute(httpPost);
            // 打印响应状态
            System.out.println("返回信息状态 :" + response.getStatusLine().getStatusCode());
            // 打印响应内容
            responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("返回内容 :" + responseBody);

        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("请求接口请求失败");
        }
        return responseBody;
    }

    public static String odPost(String url, Map<String,String> paramObj) {
        String responseBody ="";
        try {
            // 创建HttpClient实例
            HttpClient httpClient = HttpClients.createDefault();
            // 创建HttpPost实例
            HttpPost httpPost = new HttpPost(url);
            // 设置请求头部的Content-Type
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            // 创建参数列表
            List<NameValuePair> param = FormatUtil.mapToParam(paramObj);
            if(!ListUtil.isEmpty(param)){
                // 设置请求编码
                HttpEntity entity = new UrlEncodedFormEntity(param, "UTF-8");
                httpPost.setEntity(entity);
            }
            System.out.println("请求参数 : " + paramObj.toString());
            // 执行请求并获得响应
            HttpResponse response = httpClient.execute(httpPost);
            // 打印响应状态
            System.out.println("返回信息状态 : " + response.getStatusLine().getStatusCode());
            // 打印响应内容
            responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("返回内容: " + responseBody);
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("请求接口请求失败");
        }
        return responseBody;
    }

    public static String odPost(String url, OrderParam orderParam) {
        String responseBody ="";
        try {
            // 创建HttpClient实例
            HttpClient httpClient = HttpClients.createDefault();
            // 创建HttpPost实例
            HttpPost httpPost = new HttpPost(url);
            // 设置请求头部的Content-Type
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
            // 创建参数列表
            List<NameValuePair> param = FormatUtil.objToParam(orderParam);
            if(!ListUtil.isEmpty(param)){
                // 设置请求编码
                HttpEntity entity = new UrlEncodedFormEntity(param, "GB2312");
                httpPost.setEntity(entity);
            }
            System.out.println("请求参数 : " + orderParam.toString());
            // 执行请求并获得响应
            HttpResponse response = httpClient.execute(httpPost);
            // 打印响应状态
            System.out.println("返回信息状态 : " + response.getStatusLine().getStatusCode());
            if(response.getStatusLine().getStatusCode() != 200){
                throw  new RuntimeException("请求接口请求失败");
            }
            // 打印响应内容
            responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("返回内容: " + responseBody);
        }catch (Exception e){
            e.printStackTrace();
            throw  new RuntimeException("请求接口请求失败");
        }
        return responseBody;
    }



}
